<?php
/*
 * @Date: 2020-05-03 22:03:52
 * @名称: 
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑人: 初雪桜
 * @LastEditTime: 2021-03-01 22:35:12
 * @FilePath: /Site/app/useradmin/controller/Comics.php
 */

declare(strict_types=1);

namespace app\useradmin\controller;

use app\BaseController;
use app\useradmin\controller\Comics\Get\ComicsList;

use app\useradmin\controller\Comics\Get\GetComicsController;
use app\useradmin\controller\Comics\Get\GetNewPage;
use app\useradmin\controller\Comics\Get\GetSonProject;
use app\useradmin\controller\Comics\Operation\ComicsAddProject;
use app\useradmin\controller\Comics\Operation\ComicsEdit;
use think\facade\Db;
use think\facade\View;

class Comics extends BaseController
{
    /**
     * @方法名: 获取漫画列表
     * @作者: 初雪桜
     * @信息: 获取漫画列表 一个网页
     * @param Int $page
     * @param Int $seleNumber
     * @param Json $user_info
     * @return Html
     */
    public function ComicsListView()
    {
        //@q·初始化变量
        $page = input('get.page', 1); //获取当前页
        $seleNumber = 20; //每次查询的数量
        //@q·初始化变量
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        //用户信息
        $user_info = $userTokenVerificationParse['data']['user_info'];

        $ComicsList = new ComicsList($this->app);
        return $ComicsList->GetComicsList($page, $seleNumber, $user_info);
    }
    /**
     * @方法名: 获取随机一页数据
     * @作者: 初雪桜
     * @信息: 查询完漫画列表 按条件搜索 换页
     * @param Json $user_info 用户信息
     * @param Int $page 第几页
     * @param String $title 漫画标题
     * @param int $status 漫画状态
     * @return Json
     */
    public function getComicsList()
    #获取漫画列表 json
    {
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        //@v 用户信息
        $user_info = $userTokenVerificationParse['data']['user_info'];

        $page = input('get.page', 1);
        #获取当前页
        $title = input('get.title', null);
        #要搜索的标题
        $status = input('get.status', 0);
        #要跳转的页数

        $GetNewPage = new GetNewPage($this->app);
        return $GetNewPage->GetNewPage($user_info, $page, $title, $status);
    }
    /**
     * @方法名: 保存漫画信息
     * @作者: 初雪桜
     * @信息: 保存漫画信息
     * @return Json
     */
    public function SaveInformation()
    {

        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['toKen'] = input('post.toKen', null); //toKen我自己的
        $postData['bookId'] =  input('post.bookId', null); //书籍id
        $postData['title'] =  input('post.title', null); //标题
        $postData['description'] =  input('post.description', null); //简介
        $postData['comment'] =  input('post.comment', null); //comment 开关
        $postData['download'] =  input('post.download', null); //download 开关
        $postData['categories'] =  input('post.categories', null); //分类
        $postData['tags'] =  input('post.tags', null); //标签
        $postData['time'] = input('post.time', null); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId', null); //sha256的书籍id

        $hashHmac_bookId = hash_hmac("sha256",   $postData['bookId'] . $postData['time'], config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //加密后 加密id
        if ($hashHmac_bookId != $postData['verificationBookId']) { //验证书籍id
            return errorJsonReturn('Validation failed', 'SeverError'); //到这里就是验证错误 验证书籍错误
        }
        if (
            $postData['toKen'] == null ||
            $postData['bookId'] == null ||
            $postData['categories'] == null ||
            $postData['title'] == null ||
            $postData['description'] == null ||
            $postData['tags'] == null ||
            $postData['time'] == null ||
            !$postData['download'] !=  null ||
            !$postData['comment'] != null
        ) {
            return errorJsonReturn('Incomplete data saving failed'); //数据不完整
        }
        $hashHmac_toKen = hash_hmac("sha256", $token .  $postData['bookId'] . $postData['time'], config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //加密后 加密id
        if ($hashHmac_toKen != $postData['toKen']) { //判断加密的token是不是通过
            return errorJsonReturn('Validation failed', 'SeverError'); //到这里就是验证错误
        }

        $ComicsEdit = new ComicsEdit($this->app);
        return $ComicsEdit->SaveComicsEdit(
            $user_info,
            $postData
        );
    }
    /**
     * @方法名: 获取漫画子章节信息
     * @作者: 初雪桜
     * @信息: 显示某个漫画的子章节
     * @param Sting $bookId 漫画id
     * @return Json
     */
    public function getComicsSonProject()
    {
        $bookId = input('post.bookId', null); //获取当前页
        $GetSonProject = new GetSonProject($this->app);
        return $GetSonProject->GetSonProject($bookId);
    }
    /**
     * @方法名: 增加分卷
     * @作者: 初雪桜
     * @信息: 后台->漫画列表->增加分卷
     * @return {json}
     */
    public function AddProject()
    {
        $token = cookie("Authorization"); //@b 用户token
        $userTokenVerificationParse = ParseUserToken($token, true); //@b 验证用户token
        if ($userTokenVerificationParse['code'] != 200) { //@b 判断用户返回数据是否正常 200返回正确
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"]; //@b 解析用户数据
        $bookId = input('bookId'); //@b 获取书籍id
        $projectName = input('title'); //@b 获取分权标题
        $order = input('order'); //@b 获取序列
        $ComicsAddProject = new ComicsAddProject($this->app);
        return $ComicsAddProject->AddProject( $user_info, $bookId, $projectName, $order);
    }
}
